package com.haiheng.module_main.fragment.bookcar

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.haiheng.common.constant.RourePage
import com.haiheng.common.fragment.RouterFragment
import com.haiheng.library_base.adapter.BaseAdapter
import com.haiheng.library_base.dialog.LoadingDialog
import com.haiheng.library_base.model.BaseViewModel
import com.haiheng.library_base.toast.ToastUtils
import com.haiheng.module_main.R
import com.haiheng.module_main.adapter.bookcar.BookDeliveryOrderAdapter
import com.haiheng.module_main.bean.BookDeliveryOrderStatus
import com.haiheng.module_main.databinding.FragmentBookDeliveryOrderBinding
import com.haiheng.module_main.dialog.BookCarHintDialog
import com.haiheng.module_main.dialog.CancelDeliveryOrderDialog
import com.haiheng.module_main.mode.BookDeliveryOrderViewModel

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2024/8/28 0028
 * @Version: 1.0
 */
class BookDeliveryOrderFragment:RouterFragment() {

    private lateinit var _binding:FragmentBookDeliveryOrderBinding
    private val viewModel: BookDeliveryOrderViewModel by viewModels()
    private val bookCarListAdapter = BookDeliveryOrderAdapter()
    private lateinit var loadingDialog : LoadingDialog

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentBookDeliveryOrderBinding.inflate(inflater,container,false)
        return _binding.root
    }
    override fun initView() {
        loadingDialog = LoadingDialog(requireContext(), R.style.AvLoadingDialogStyle)
        _binding.includeTile.tvTitle.setText("送书订单")
        _binding.includeTile.tvClear.visibility = View.VISIBLE
        _binding.includeTile.tvClear.setTextColor(activity.getColor(R.color.color_212121))
        _binding.includeTile.tvClear.setText("未处理：0 个")
        _binding.includeTile.sflBack.setOnClickListener {
            activity.navigation(RourePage.BOOK_CAR_LOGIN_RESULT)
        }

        _binding?.rvOrderList?.layoutManager = LinearLayoutManager(activity)
        _binding?.rvOrderList?.adapter = bookCarListAdapter

        bookCarListAdapter.setOnItemChildClickListener(object :BaseAdapter.OnItemChildClickListener{
            override fun onItemChildClick(
                view: View,
                holder: BaseAdapter.ViewBindHolder,
                position: Int
            ) {
                var itemData = bookCarListAdapter.getItem(position)
                when(view.id){
                    R.id.slDeliveryBook->{
                        BookCarHintDialog.newInstance()
                            .setHintMsg("是否确认送书")
                            .setOnDialogListener(object :BookCarHintDialog.OnDialogClickListener{
                                override fun onLeftClick(dialog: BookCarHintDialog) {
                                    dialog.dismiss()
                                }

                                override fun onRightClick(dialog: BookCarHintDialog) {
                                    loadingDialog.show()
//                                    viewModel.changeStaus(itemData.studyDeskDeliverBookOrderId.toString(),"3")
                                    viewModel.changeOrderStatus(BookDeliveryOrderStatus.COMPLETED,itemData.orderNumber.toString())
                                    dialog.dismiss()
                                }
                            }).show(childFragmentManager)
                    }
                    R.id.slNotDeliveryBook->{
                        CancelDeliveryOrderDialog.newInstance()
                            .setOnDialogListener(object : CancelDeliveryOrderDialog.OnDialogClickListener{
                                override fun onLeftClick(dialog: CancelDeliveryOrderDialog) {
                                    dialog.dismiss()
                                }

                                override fun onRightClick(
                                    resean: String,
                                    selectReson: String,
                                    dialog: CancelDeliveryOrderDialog
                                ) {

                                    loadingDialog.show()
//                                    viewModel.changeStaus(itemData.studyDeskDeliverBookOrderId.toString(),"4",resean,selectReson)
                                    viewModel.changeOrderStatus(BookDeliveryOrderStatus.CANCELLED,itemData.orderNumber.toString(),resean)
                                    dialog.dismiss()
                                }
                            }).show(childFragmentManager)

                    }
                }
            }
        })
    }

    override fun initViewModel(): BaseViewModel? {
        viewModel.bookInfoList.observe(viewLifecycleOwner){
            loadingDialog.hide()
            bookCarListAdapter.setNewData(it)
            _binding.includeTile.tvClear.setText("未处理：${it.size} 个")

        }
        viewModel.faiMsg.observe(viewLifecycleOwner){
            loadingDialog.hide()
            ToastUtils.showShort(it)
        }
       return viewModel
    }

    override fun initLoad() {
        loadingDialog.show()
        viewModel.getOrderList()
    }
}